package practise.alogrithm.introductionToAlogrithm.dataStructures;

import practise.alogrithm.introductionToAlogrithm.ListNode;

/**
 * Created by Richard on 7/20/2014.
 */
public class LinkedStack<T> {
    ListNode<T> stackHead;

    public void push(T object){
        if(stackHead!=null){
            ListNode<T> newHead = new ListNode<>();
            newHead.setNext(stackHead);
            newHead.setValue(object);
            stackHead= newHead;

        }

        else {
            stackHead = new ListNode<>();
            stackHead.setValue(object);
        }

    }

    public T pop(){
        if(stackHead==null){
            throw  new IndexOutOfBoundsException();
        }
        T element = stackHead.getValue();
        stackHead = stackHead.getNext();
        return element;
    }

    public static void main(String[] args){
        LinkedStack stack = new LinkedStack();
        for(int i=0; i<7;i++){
            stack.push(i);
        }

        for(int i=0;i<8;i++){
            System.out.print("["+stack.pop()+"]");
        }

    }
}
